package fiveGame.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;

import fiveGame.Util.StaticTool;
import fiveGame.Util.TiangleUtil;
import fiveGame.dao.Chess;
import fiveGame.element.Chesses;
import fiveGame.element.SaveElement;

/**
 * 
 * @author Tiangle
 * 
 * @version 创建时间：2021年9月4日 下午2:02:32
 * 
 * 
 * 
 */
public class ChessDao implements Chess,Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	TiangleUtil t = new TiangleUtil();
	LinkedList<Chesses> ch = new LinkedList<Chesses>();
	public static int isBlack = 1;
	public static int blackr = 0;
	public static int whiter = 0;
	public static int canRe = 0;
	File f = new File("src\\save");
	@Override
	public void show() {
		// TODO Auto-generated method stub
		for (int j = StaticTool.firstY; j <= StaticTool.xyLen * StaticTool.yLength; j += StaticTool.xyLen) {
			for (int i = StaticTool.firstX; i <= StaticTool.xyLen * StaticTool.xLength; i += StaticTool.xyLen) {
				if (hasChess(new Chesses(i, j))) {
					int c = getChess(i, j).getColor();
					System.out.print((c == 1 ? "(黑,棋)" : "(白,棋)") + "  \t");
				} else {
					System.out.print("(" + i + "," + j + ")  \t");
				}

			}
			System.out.println();
		}
	}

	@Override
	public boolean hasChess(Chesses c) {
		// TODO Auto-generated method stub
		if (ch == null)
			return false;
		for (Chesses x : ch) {
			if (c.equals(x)) {
				return true;
			}
		}
		return false;
	}

	public Chesses getChess(int x, int y) {
		if (ch == null)
			return null;
		for (Chesses a : ch) {
			if (a.equals(new Chesses(x, y))) {
				return a;
			}
		}
		return null;
	}

	@Override
	public boolean isWin(Chesses c) {
		// TODO Auto-generated method stub
		int h = 1;
		int s = 1;
		int p = 1;
		int l = 1;
		for (int i = 1; i < 5; i++) {
			if (move(c, i, 0) == null)
				break;
			if (move(c, i, 0).getColor() == c.getColor()) {
				h++;
			} else {
				break;
			}
		}
		for (int i = -1; i > -5; i--) {
			if (move(c, i, 0) == null)
				break;
			if (move(c, i, 0).getColor() == c.getColor()) {
				h++;
			} else {
				break;
			}
		}
		for (int i = 1; i < 5; i++) {
			if (move(c, 0, i) == null)
				break;
			if (move(c, 0, i).getColor() == c.getColor()) {
				s++;
			} else {
				break;
			}
		}
		for (int i = -1; i > -5; i--) {
			if (move(c, 0, i) == null)
				break;
			if (move(c, 0, i).getColor() == c.getColor()) {
				s++;
			} else {
				break;
			}
		}
		for (int i = 1; i < 5; i++) {
			if (move(c, i, i) == null)
				break;
			if (move(c, i, i).getColor() == c.getColor()) {
				p++;
			} else {
				break;
			}
		}
		for (int i = -1; i > -5; i--) {
			if (move(c, i, i) == null)
				break;
			if (move(c, i, i).getColor() == c.getColor()) {
				p++;
			} else {
				break;
			}
		}
		for (int i = 1; i < 5; i++) {
			if (move(c, -i, i) == null)
				break;
			if (move(c, -i, i).getColor() == c.getColor()) {
				l++;
			} else {
				break;
			}
		}
		for (int i = -1; i > -5; i--) {
			if (move(c, -i, i) == null)
				break;
			if (move(c, -i, i).getColor() == c.getColor()) {
				l++;
			} else {
				break;
			}
		}
		if (h >= 5 || s >= 5 || p >= 5||l>=5) {
			return true;
		}

		System.out.println("h:" + h + "\ns:" + s + "\np:" + p);
		return false;
	}

	public Chesses move(Chesses c, int x, int y) {
		Chesses cc = new Chesses(c.getX() + StaticTool.xyLen * x, c.getY() + y * StaticTool.xyLen);
		if (hasChess(cc)) {
			return getChess(cc.getX(), cc.getY());
		}
		return null;
	}

	@Override
	public void downChess() {
		// TODO Auto-generated method stub
		while (true) {
			Chesses newC = inputC();
			if (!hasChess(newC)) {
				newC.setColor(isBlack);
				ch.addLast(newC);
				canRe++;
				if (isBlack == 1)
					isBlack = 2;
				else
					isBlack = 1;
				break;
			} else {
				System.out.println("此处有子！重新下");
			}
		}

	}

	public boolean winwin() {
		if (ch.getLast() == null)
			return false;
		return isWin(ch.getLast());
	}

	public Chesses inputC() {
		System.out.println("请输入x：");
		int x = t.getInt(StaticTool.firstX, StaticTool.xLength * StaticTool.xyLen);
		System.out.println("请输入y：");
		int y = t.getInt(StaticTool.firstY, StaticTool.yLength * StaticTool.xyLen);

		int x1 = (x - StaticTool.firstX) / StaticTool.xyLen;
		int y1 = (y - StaticTool.firstY) / StaticTool.xyLen;
		if ((x - StaticTool.firstX) % StaticTool.xyLen >= StaticTool.xyLen / 2) {
			x1 = (x1 + 1) * StaticTool.xyLen + StaticTool.firstX;
		} else {
			x1 = x1 * StaticTool.xyLen + StaticTool.firstX;
		}
		if ((y - StaticTool.firstY) % StaticTool.xyLen >= StaticTool.xyLen / 2) {
			y1 = (y1 + 1) * StaticTool.xyLen + StaticTool.firstY;
		} else {
			y1 = y1 * StaticTool.xyLen + StaticTool.firstY;
		}
		return new Chesses(x1, y1);
	}

	@Override
	public boolean revoke() {
		// TODO Auto-generated method stub

		if (((isBlack == 2&&blackr<=StaticTool.re)||(isBlack==1&&whiter<=StaticTool.re))&&canRe>=2) {
			return true;
		}
		return false;
	}
	public void reee() {
		ch.removeLast();
		if (isBlack == 1) {
			whiter++;
			isBlack = 2;
		}
			
		else {
			isBlack = 1;
			blackr++;
		}
			
		canRe = 0;
	}
	public String oldFile() {
		String[] ss = f.list();
		String s;
		while(true) {
			System.out.println("请输入存档信息：");
			s = t.getString();
			if(ss!=null) {
				for (String string : ss) {
					if(string.equals(s)) {
						System.out.println("已有该条存档");
						continue;
					}
				}
			}
			
			break;
		}
		return s;
	}
	public void save() throws IOException {
		String s = oldFile();
		File ff = new File(f,s+".end");
		ff.createNewFile();
		FileOutputStream fs = new FileOutputStream(ff);
		ObjectOutputStream os = new ObjectOutputStream(fs);
		SaveElement se = new SaveElement(this.ch,isBlack,blackr,whiter,canRe);
		os.writeObject(se);
		os.close();
		
	}
	public void old(String s) throws IOException, ClassNotFoundException {
		File ff = new File(f,s);
		FileInputStream fs = new FileInputStream(ff);
		ObjectInputStream os = new ObjectInputStream(fs);
		SaveElement c = (SaveElement) os.readObject();
		flush(c);
	}
	public ArrayList<String> showOld() {
		String[] ss = f.list();
		if(ss == null)
			return null;
		ArrayList<String> as = new ArrayList<String>();
		for(String s:ss) {
			if(s.endsWith(".end")) {
				as.add(s);
			}
		}
		return as;
	}
	public void flush(SaveElement c) {
		this.ch = c.ch;
		this.blackr = c.blackr;
		this.whiter = c.whiter;
		this.isBlack = c.isBlack;
		this.canRe = c.canRe;
	}
}
